#---------------------------------------------------------------------------
#!/bin/sh
# Program:
# 	Install Packages For Archlinux after Chroot
# History:
#	2014/05/01 Potter Version 3.0
#---------------------------------------------------------------------------
# Instruction:
#	This program is free software: you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as publish by the Free 
#  Software Fundation
#   This program is distribute in the hope that it will be useful, easy
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTUCULAR PURPOSE. See the
#  GUN General Publish License for more details.
#
#   You should have received a copy of the GNU General Publish License
#  along with this program. If not, see <http://www.gnu.org/licenses/>
#---------------------------------------------------------------------------
####
#  You need User root
#  Root
if [ "$(id -u)" -ne "0" ]
then
	echo "Error: You Need to be Root."
	exit
fi

# Log File
logfile=$HOME/InstallLog/archlinux_install.log
logdir=$HOME/InstallLog
rm -rf $logdir
mkdir $logdir && touch $logfile


# Function
# Download Log
download_log() {
	for argv in "$@"; do
		pacman -S --noconfirm $argv && echo "Succeed in installing --> $argv" >> $logfile || echo "ERROR: failed to install --> $argv" >> $logfile
	done
}

yaourt_log() {
	for argv in "$@"; do
		yaourt -S --noconfirm $argv && echo "Succeed in installing --> $argv" >> $logfile || echo "ERROR: failed to install --> $argv" >> $logfile
	done
}

# Device

# user
user=potter

# 1.passwd 
echo "******************************************************"
echo "* Root Password is: potter                           *"
echo "*(Please Change Password for root after Installation)*"
echo "******************************************************"
passwd root << EOF
potter
potter
EOF
# New user
echo "*******************************************************"
echo "* New General user : potter                           *"
echo "*(Please Change Password for $user after Installation)*"
echo "*******************************************************"
# read -p "Please Enter User Name(Default: $user): " user
useradd -m -g users -s /bin/bash $user
echo "$user Password: "
passwd $user << EOF
potter
potter
EOF

# add $user to root group
echo "$user ALL=(ALL) ALL" >> /etc/sudoers

# 2 vim
download_log vim

# wget
download_log wget

#2.1 grub
download_log grub

# pacstrap genfstab arch-chroot
cd /usr/bin
wget -O pacstrap http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/pacstrap && chmod +x pacstrap && \
wget -O genfstab http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/genfstab && chmod +x genfstab && \
wget -O arch-chroot http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/arch-chroot && chmod +x arch-chroot && \
cd $logdir


# 3
echo "WenAway" > /etc/hostname # hostname
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # localtime
# locale
if [ ! -f "/etc/locale.gen.example" ]
then
	mv /etc/locale.gen /etc/locale.gen.example
fi
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
echo "zh_CN.UTF-8 UTF-8" >> /etc/locale.gen
echo "zh_CN.GBK GBK" >> /etc/locale.gen
echo "zh_CN GB2312" >> /etc/locale.gen
locale-gen

# 4. 关闭小喇叭
echo "blacklist pcspkr" > /etc/modprobe.d/nopcspkr.conf

# 5. X server
echo "************" >> $logfile
echo "* X server *" >> $logfile
echo "************" >> $logfile
download_log xorg-server xorg-xinit xorg-utils xorg-server-utils mesa

# 6. VGA
echo "***********" >> $logfile
echo "*   VGA   *" >> $logfile
echo "***********" >> $logfile
download_log xf86-video-vesa xf86-video-intel nouveau-dri
vgaType=$(lspci | grep -i vga | grep -i 'AMD' | wc -l)
if [ "$vgaType" != "0" ]
then
	download_log xf86-video-ati
else
	# Nvidia
	download_log xf86-video-nouveau
fi

# 7.Labtop Touchpad
download_log xf86-input-synaptics

# 8.Test X
# download_log xorg-twm xorg-xclock xterm

# 9.DE
# pacman -S --noconfirm kde # kde Maybe Too large
# pacman -S --noconfirm gnome # gnome
# pacman -S --noconfirm xfce # xfce
#
# echo "exec startkde" >> $HOME/.xinitrc
# echo "exec gnome-session" >> $HOME/.xinitrc
# echo "exec startxfce4" >> $HOME/.xinitrc
# $HOME/.xinitrc
cp /etc/skel/.xinitrc $HOME/.xinitrc
# 
IsVirtualBox=$(lspci | grep -i virtualbox | wc -l)
if [ "$IsVirtualBox" != "0" ]; then
	echo "**************" >> $logfile
	echo "* VirtualBox *" >> $logfile
	echo "**************" >> $logfile
	echo "exec startkde" >> $HOME/.xinitrc
	download_log kde
elif [ "sdx" = "sda" ]; then
	# sdx will be replaced by chroot before file
	echo "***************************" >> $logfile
	echo "* Your Computer Hard Disk *" >> $logfile
	echo "***************************" >> $logfile
	echo "exec startkde" >> $HOME/.xinitrc
	download_log kde
else
	echo "*******************" >> $logfile
	echo "* Your Flash Disk *" >> $logfile
	echo "*******************" >> $logfile
	echo "exec startxfce4" >> $HOME/.xinitrc
	download_log xfce4 # xfce4
fi

# 10.Input Method
download_log fcitx
# download_log kcm-fcitx
download_log fcitx-googlepinyin
# download_log fcitx-im
# 在.xinitrc加入
fcitx=$(grep fcitx $HOME/.xinitrc | wc -l)
if [ "$fcitx" = "0" ]
then
	echo "export XIM=fcitx" >> $HOME/.xinitrc
	echo "export XMODIFIERS=\"@im=fcitx\"" >> $HOME/.xinitrc
	echo "export GTK_IM_MODULE=fcitx" >> $HOME.xinitrc
	echo "export QT_IM_MODULE=fcitx" >> $HOME/.xinitrc
	echo "export XIM_PROGRAM=fcitx" >> $HOME/.xinitrc
fi

# add .xinitrc to $user home
cp $HOME/.xinitrc /home/$user/.xinitrc

# 12.Firefox flashplugin
download_log firefox flashplugin

# 13.yaourt
echo "[archlinuxfr]" >> /etc/pacman.conf
echo "SigLevel = Never" >> /etc/pacman.conf
echo "Server = http://repo.archlinux.fr/\$arch" >> /etc/pacman.conf
pacman -Syy
download_log yaourt

# 14.google-chrome
# yaourt_log google-chrome

# 15.
# Programming:
download_log gcc codeblocks ipython python-virtualenv python-pip
# Django == 1.5.5
pip install django==1.5.5 && echo "Succeed in install --> django=1.5.5(by pip)" >> $logfile || \
echo "ERROR: failed to install --> django==1.5.5(by pip)" >> $logfile

# OpenGL
download_log freeglut

# openssh
download_log openssh

# FTP (vsftpd)
download_log vsftpd

# git
download_log git 
# download_log gitk

# subversion
download_log subversion

# net-tools
download_log net-tools

# dnsutils
download_log dnsutils

# fonts
download_log ttf-dejavu wqy-zenhei

# njit8021xclient
# Need http_proxy: (goagent)
#	http_proxy=127.0.0.1:8087 yaourt -S --noconfirm njit8021xclient
yaourt_log njit8021xclient
type njit-client
if [ "$?" != "0" ]; then
	wget -O /usr/bin/njit-client https://gitcafe.com/Potter/Softwares/raw/master/Njit-client/njit-client && \
	chmod +x /usr/bin/njit-client && \
	ln -s /usr/lib/libpcap.so.1 /usr/lib/libpcap.so.0.8 && \
	echo "Succeed in downloading Binary File --> njit-client" >> $logfile
fi

# goagent
yaourt_log goagent && wget -O /usr/share/goagent/server/uploader.sh http://git.oschina.net/sunnypotter/goagent/raw/master/server/uploader.sh

# iceplayer
yaourt_log iceplayer

# mkfs.ntfs
download_log ntfs-3g

# wifi (wpa_supplient)
download_log wpa_supplicant wpa_supplicant_gui

# 16.SystemCtl
systemctl enable dhcpcd.service
# systemctl enable kdm

# 17.pacman.conf : multilib (32位的库) (去掉注释)
# vim /etc/pacman.conf
# cd /etc
echo "[multilib]" >> /etc/pacman.conf
echo "Include = /etc/pacman.d/mirrorlist" >> /etc/pacman.conf
pacman -Syy

# 18.安装pdf阅读器
# pacman -S --noconfirm evince
# 或者pacman -S --noconfirm epdfview

# 19.dropbox
# pacman -S --noconfirm dropbox

# Usb Support (Flash disk)
cd /etc
if [ ! -f "mkinitcpio.conf.bac" ]
then
	cp mkinitcpio.conf mkinitcpio.conf.bac
fi
old_var="udev autodetect modconf block"
new_var="udev usb block autodetect modconf"
if [ "sdx" != "sda" ]; then
	sed -i "s/$old_var/$new_var/g" mkinitcpio.conf && \
	mkinitcpio -p linux
else
	mkinitcpio -p linux
fi
cd $logdir

# .vimrc
wget -O $HOME/.vimrc http://git.oschina.net/sunnypotter/Old/raw/master/notes/myvimrc.notes && \
cp $HOME/.vimrc /home/$user/.vimrc

# .Install grub on /dev/sda
grub-install --target=i386-pc --no-floppy --recheck --debug --force /dev/sdx
grub-mkconfig -o /boot/grub/grub.cfg

# Log File
echo "****************************" >> $logfile
echo "* Username    *   Password *" >> $logfile
echo "****************************" >> $logfile
echo "* root        *   potter   *" >> $logfile
echo "****************************" >> $logfile
echo "* potter      *   potter   *" >> $logfile
echo "****************************" >> $logfile
cat $logfile

# Reboot 
# exit
# umount -R /mnt
# reboot

exit 0
